package interview;

import java.util.HashMap;
import java.util.Map;

//面试题 01.06. 字符串压缩
public class Num01_06_compressString {

    public String compressString(String S) {
        StringBuilder sb = new StringBuilder();
        if (S.length()==0){
            return S;
        }
        int count=1;
        char ch = S.charAt(0);
        for (int i = 1; i < S.length()  ; i++) {
            if (ch==S.charAt(i)){
                count++;
            }else {
                sb.append(ch);
                sb.append(count);
                ch=S.charAt(i);
                count=1;
            }

        }
        sb.append(ch);
        sb.append(count);
        return sb.length()>=S.length()?S:sb.toString();
    }
}
